package com.example.myprojectoffeixue;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import org.apache.http.entity.StringEntity;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
//本界面是报警日志界面
public class BaoJingActivity extends Activity {
    private ListView actualListView;
    private ArrayList<Websocketutil> mencilist = new ArrayList<>();
    private ArrayList<Websocketutil> hongwailist = new ArrayList<>();
    private ArrayList<Websocketutil> mensuolist = new ArrayList<>();
    private MyApplication m;
    HongWaiAdapter b;
    BjListadapter b2;
    BjListadapter c;
    Dialog dialog1;
    String flag;
    PullToRefreshListView kt_group;
    LinearLayout item;
    int x=0;
    int f=0;
    int z=0;
    int total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bao_jing);
        info();
    }
    public void info() {
        item= (LinearLayout) findViewById(R.id.item);
        Intent it = getIntent();
        flag = it.getStringExtra("flag");
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_my_baojing);
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        kt_group = (PullToRefreshListView) findViewById(R.id.baojinglist);
        kt_group.setMode(PullToRefreshBase.Mode.BOTH);
        actualListView = kt_group.getRefreshableView();
        kt_group.getLoadingLayoutProxy(false, true).setPullLabel("正在加载...");
        kt_group.getLoadingLayoutProxy(false, true).setReleaseLabel("松开加载更多...");
        // 设置PullRefreshListView下拉加载时的加载提示
        kt_group.getLoadingLayoutProxy(true, false).setPullLabel("正在刷新...");
        kt_group.getLoadingLayoutProxy(true, false).setReleaseLabel("松开刷新...");
        kt_group.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                                          public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                                              init(flag,false);
                                          }
                                          public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                                              if("mensuo".equals(flag)){
                                                  addmore();}else if("hongwai".equals(flag)){
                                                  addmore2();
                                              }else if("menci".equals(flag)){
                                                  addmore3();
                                              }
                                          }
                                      }
        );
        dialog1 = new Dialog(BaoJingActivity.this, R.style.myDialogTheme);
        dialog1.setContentView(R.layout.progressdialog);
        m = (MyApplication) getApplication();
        init(flag,true);
    }

public void  init(String flag ,boolean flag1) {

    if ("menci".equals(flag)) {
        f=0;
        mencilist.clear();
        item.setVisibility(View.VISIBLE);
        if(flag1){
        dialog1.show();}
        GateWay y = new GateWay();
        y.userId = m.userId;
        y.offset="0";
        y.limit="10";
        String j = JSON.toJSONString(y);
        Log.d("cmd",j);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getMagnet", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                      if(arg0.result!= null && !"".equals(arg0.result)) {
                          arg0.result = AESUtil.decrypt(arg0.result);
                          Log.d("res",arg0.result);
                          JSONObject js = JSONObject.parseObject(arg0.result);
                          String status = js.getString("status");
                          if ("0".equals(status)) {
                              JSONObject ja = js.getJSONObject("magnetList");
                              JSONArray ja2=ja.getJSONArray("rows");
                              ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja2.toString(), Websocketutil.class);
                              for (int i = 0; i < list.size(); i++) {
                                  for (int j = 0; j < m.lsb4.size(); j++) {
                                      String flag1 = list.get(i).ieee + list.get(i).port;
                                      String flag2 = m.lsb4.get(j).ieee + m.lsb4.get(j).port;
                                      if (flag1.equals(flag2)) {
                                          if (m.lsb4.get(j).deviceName != null) {
                                              list.get(i).deviceName = m.lsb4.get(j).deviceName;
                                          } else {
                                              list.get(i).deviceName = "门磁";
                                          }
                                      }
                                  }
                                  mencilist.add(list.get(i));
                              }

                              if (b2 == null) {
                                  b2 = new BjListadapter(BaoJingActivity.this, mencilist, 1);
                                  actualListView.setAdapter(b2);
                                  dialog1.dismiss();
                              } else {
                                  b2.notifyDataSetChanged();
                                  kt_group.onRefreshComplete();
                              }


                          } else {
                              ToastUtils.showToast(BaoJingActivity.this, "服务器错误");
                          }
                      }else{
                                 Log.d("4444444444444","66666");
                          ToastUtils.showToast(BaoJingActivity.this, "服务器错误");
                      }
                }});


    } else if ("hongwai".equals(flag)) {
        z=0;
        hongwailist.clear();
        item.setVisibility(View.GONE);
        if(flag1){
        dialog1.show();}
        GateWay y = new GateWay();
        y.userId = m.userId;
        y.offset="0";
        y.limit="10";
        String j = JSON.toJSONString(y);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getInfraredInfo", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if(arg0.result != null && !"".equals(arg0.result)){
                        arg0.result = AESUtil.decrypt(arg0.result);
                        JSONObject js = JSONObject.parseObject(arg0.result);
                        String status=js.getString("status");
                        if("0".equals(status)){
                        JSONObject ja = js.getJSONObject("infraredList");
                        JSONArray ja2=ja.getJSONArray("rows");
                        ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja2.toString(), Websocketutil.class);
                            for (int i = 0; i < list.size(); i++) {
                                for(int j=0;j<m.lsb2.size();j++){
                                    String flag1=list.get(i).ieee+list.get(i).port;
                                    String flag2=m.lsb2.get(j).ieee+m.lsb2.get(j).port;
                                    if(flag1.equals(flag2)){
                                        if(m.lsb2.get(j).deviceName!=null){
                                            list.get(i).deviceName=m.lsb2.get(j).deviceName;}else{
                                            list.get(i).deviceName="红外";
                                        }
                                    }
                                }
                                hongwailist.add(list.get(i));

                            }
                        if (b == null) {
                            b = new HongWaiAdapter(BaoJingActivity.this, hongwailist);
                            actualListView.setAdapter(b);
                            dialog1.dismiss();
                        } else {
                            b.notifyDataSetChanged();
                            kt_group.onRefreshComplete();
                        }
                    }else{
                        HttpConnection.showToast(BaoJingActivity.this,"服务器错误");

                    }}else{
                            HttpConnection.showToast(BaoJingActivity.this,"服务器错误");
                        }
                    }

                });
    }else if("mensuo".equals(flag)){
        x=0;
        mensuolist.clear();
        item.setVisibility(View.VISIBLE);
        if(flag1){
            dialog1.show();}
        GateWay y = new GateWay();
        y.authId = m.authId;
        y.offset="0";
        y.limit="10";
        y.type="1";
        String j = JSON.toJSONString(y);
        Log.d("cmd",j);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getGateLock", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if(arg0.result != null && !"".equals(arg0.result)){
                            arg0.result = AESUtil.decrypt(arg0.result);
                            Log.d("门锁",arg0.result);
                            JSONObject js = JSONObject.parseObject(arg0.result);
                            String status=js.getString("status");
                            if("0".equals(status)){
                                JSONObject js2=js.getJSONObject("logList");
                                JSONArray ja = js2.getJSONArray("rows");
                                ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja.toString(), Websocketutil.class);
                                for (int i = 0; i < list.size(); i++) {
                                    for(int j=0;j<m.lsb1.size();j++){
                                        String flag1=list.get(i).ieee+list.get(i).port;
                                        String flag2=m.lsb1.get(j).ieee+m.lsb1.get(j).port;
                                        if(flag1.equals(flag2)){
                                            if(m.lsb1.get(j).deviceName!=null){
                                                list.get(i).deviceName=m.lsb1.get(j).deviceName;}else{
                                                list.get(i).deviceName="门锁";
                                            }
                                        }
                                    }
                                   mensuolist.add(list.get(i));
                                }
                                if (c == null) {
                                    c = new BjListadapter(BaoJingActivity.this,mensuolist,2);
                                    actualListView.setAdapter(c);
                                    dialog1.dismiss();
                                } else {
                                    c.notifyDataSetChanged();
                                    kt_group.onRefreshComplete();
                                }

                            }else{
                                HttpConnection.showToast(BaoJingActivity.this,"服务器错误");
                            }}else{
                            HttpConnection.showToast(BaoJingActivity.this,"服务器错误");
                        }
                    }

                });

    }


}

    public void addmore(){
        x++;
        GateWay y = new GateWay();
        y.authId = m.authId;
        y.offset=0+10*x+"";
        y.limit="10";
        y.type="1";
        String j = JSON.toJSONString(y);
        Log.d("cmd1",j);
        j = AESUtil.encrypt(j);
        HttpUtils httpUtils = new HttpUtils();
        RequestParams params = new RequestParams();
        params.setHeader("Content-Type", "application/json");
        try {
            params.setBodyEntity(new StringEntity(j, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getGateLock", params,
                new RequestCallBack<String>() {

                    @Override
                    public void onFailure(HttpException arg0, String arg1) {
                        Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> arg0) {
                        if(arg0.result != null && !"".equals(arg0.result)){
                            arg0.result = AESUtil.decrypt(arg0.result);
                            JSONObject js = JSONObject.parseObject(arg0.result);
                            String status=js.getString("status");
                            if("0".equals(status)){
                                JSONObject js2=js.getJSONObject("logList");
                                total=js2.getInteger("total");
                                JSONArray ja = js2.getJSONArray("rows");
                                ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja.toString(), Websocketutil.class);
                                    for (int i = 0; i < list.size(); i++) {
                                        for (int j = 0; j < m.lsb1.size(); j++) {
                                            String flag1 = list.get(i).ieee + list.get(i).port;
                                            String flag2 = m.lsb1.get(j).ieee + m.lsb1.get(j).port;
                                            if (flag1.equals(flag2)) {
                                                if (m.lsb1.get(j).deviceName != null) {
                                                    list.get(i).deviceName = m.lsb1.get(j).deviceName;
                                                } else {
                                                    list.get(i).deviceName = "门锁";
                                                }
                                            }
                                        }
                                        mensuolist.add(list.get(i));
                                    }
                               if(list.size()==0){

                                   ToastUtils.showToast(BaoJingActivity.this,"已加载全部记录");
                               }
                                if (c == null) {
                                    c = new BjListadapter(BaoJingActivity.this,mensuolist,2);
                                    actualListView.setAdapter(c);
                                    dialog1.dismiss();
                                } else {
                                    c.notifyDataSetChanged();
                                    kt_group.onRefreshComplete();
                                }





                            }else{
                                HttpConnection.showToast(BaoJingActivity.this,"服务器错误");
                            }}else{
                            HttpConnection.showToast(BaoJingActivity.this,"服务器错误");
                        }
                    }

                });}




        public void addmore2() {
            z++;
            GateWay y = new GateWay();
            y.userId = m.userId;
            y.offset = 0 + 10 * z + "";
            y.limit = "10";
            String j = JSON.toJSONString(y);
            Log.d("cmd1",j);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getInfraredInfo", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !"".equals(arg0.result)) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                JSONObject js = JSONObject.parseObject(arg0.result);
                                String status = js.getString("status");
                                if ("0".equals(status)) {
                                    JSONObject js2 = js.getJSONObject("infraredList");
                                    total = js2.getInteger("total");
                                    JSONArray ja = js2.getJSONArray("rows");
                                    ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja.toString(), Websocketutil.class);
                                    for (int i = 0; i < list.size(); i++) {
                                        for (int j = 0; j < m.lsb1.size(); j++) {
                                            String flag1 = list.get(i).ieee + list.get(i).port;
                                            String flag2 = m.lsb1.get(j).ieee + m.lsb1.get(j).port;
                                            if (flag1.equals(flag2)) {
                                                if (m.lsb1.get(j).deviceName != null) {
                                                    list.get(i).deviceName = m.lsb1.get(j).deviceName;
                                                } else {
                                                    list.get(i).deviceName = "红外";
                                                }
                                            }
                                        }
                                        hongwailist.add(list.get(i));
                                    }
                                    if (list.size() == 0) {

                                        ToastUtils.showToast(BaoJingActivity.this, "已加载全部记录");
                                    }
                                    if (b == null) {
                                        b = new HongWaiAdapter(BaoJingActivity.this, hongwailist);
                                        actualListView.setAdapter(b);
                                        dialog1.dismiss();
                                    } else {
                                        b.notifyDataSetChanged();
                                        kt_group.onRefreshComplete();
                                    }
                                } else {
                                    HttpConnection.showToast(BaoJingActivity.this, "服务器错误");
                                }
                            } else {
                                HttpConnection.showToast(BaoJingActivity.this, "服务器错误");
                            }
                        }

                    });


        }
        public void addmore3() {
            f++;
            GateWay y = new GateWay();
            y.userId = m.userId;
            y.offset = 0 + 10 * f + "";
            y.limit = "10";
            String j = JSON.toJSONString(y);
            j = AESUtil.encrypt(j);
            HttpUtils httpUtils = new HttpUtils();
            RequestParams params = new RequestParams();
            params.setHeader("Content-Type", "application/json");
            try {
                params.setBodyEntity(new StringEntity(j, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            httpUtils.send(HttpRequest.HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/user/getMagnet", params,
                    new RequestCallBack<String>() {

                        @Override
                        public void onFailure(HttpException arg0, String arg1) {
                            Toast.makeText(getBaseContext(), "网络异常", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onSuccess(ResponseInfo<String> arg0) {
                            if (arg0.result != null && !"".equals(arg0.result)) {
                                arg0.result = AESUtil.decrypt(arg0.result);
                                JSONObject js = JSONObject.parseObject(arg0.result);
                                String status = js.getString("status");
                                if ("0".equals(status)) {
                                    JSONObject js2 = js.getJSONObject("magnetList");
                                    total = js2.getInteger("total");
                                    JSONArray ja = js2.getJSONArray("rows");
                                    ArrayList<Websocketutil> list = (ArrayList<Websocketutil>) JSON.parseArray(ja.toString(), Websocketutil.class);
                                    for (int i = 0; i < list.size(); i++) {
                                        for (int j = 0; j < m.lsb1.size(); j++) {
                                            String flag1 = list.get(i).ieee + list.get(i).port;
                                            String flag2 = m.lsb1.get(j).ieee + m.lsb1.get(j).port;
                                            if (flag1.equals(flag2)) {
                                                if (m.lsb1.get(j).deviceName != null) {
                                                    list.get(i).deviceName = m.lsb1.get(j).deviceName;
                                                } else {
                                                    list.get(i).deviceName = "门磁";
                                                }
                                            }
                                        }
                                        mencilist.add(list.get(i));
                                    }
                                    if (list.size() == 0) {

                                        ToastUtils.showToast(BaoJingActivity.this, "已加载全部记录");
                                    }
                                    if (b2 == null) {
                                        b2 = new BjListadapter(BaoJingActivity.this, mencilist, 1);
                                        actualListView.setAdapter(b2);
                                        dialog1.dismiss();
                                    } else {
                                        b2.notifyDataSetChanged();
                                        kt_group.onRefreshComplete();
                                    }
                                } else {
                                    HttpConnection.showToast(BaoJingActivity.this, "服务器错误");
                                }
                            } else {
                                HttpConnection.showToast(BaoJingActivity.this, "服务器错误");
                            }
                        }

                    });


        }























}
